package com.admin.action.boraction;

import com.admin.Action;
import com.admin.service.borservice.BorServiceImpl;
import com.borrower.dto.BorrowInfoBean;
import com.google.gson.Gson;
import com.pojo.BorrowerBean;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class GetBorrowInfoAction extends Action {
    Gson gson = new Gson();

    @Override
    public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        PrintWriter out=response.getWriter();
        HttpSession session = request.getSession();
        if(session.getAttribute("adminInfo")==null){
            out.println("<script>parent.window.location.href='error.html';</script>");
            return;
        }

        BorServiceImpl service = new BorServiceImpl();
        BorrowInfoBean bor = new BorrowInfoBean();
        if (request.getParameter("userName") != null) {
            bor.setUserName(request.getParameter("userName"));
        } else {
            bor.setUserName("");
        }
        if (request.getParameter("state") != null) {
            bor.setState(request.getParameter("state"));
        } else {
            bor.setState("");
        }
        List<BorrowInfoBean> list = service.getBorrowInfoService(bor);
        String allBorrowInfoJson = gson.toJson(list);

        out.print(allBorrowInfoJson);
    }
}
